package com.regex.web.baidu.ai.image;

import java.net.URLEncoder;

import com.regex.web.baidu.ai.AuthService;
import com.regex.web.baidu.ai.Base64Util;
import com.regex.web.baidu.ai.FileUtil;
import com.regex.web.baidu.ai.HttpUtil;
import com.regex.web.baidu.ai.NetImageUtils;

/**
 * 通用物体和场景识别
 * @author ocean.regex
 *
 */
public class AdvancedGeneral {
	/**
	    * 重要提示代码中所需工具类  本地图片识别
	    * FileUtil,Base64Util,HttpUtil,GsonUtils请从
	    * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
	    * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
	    * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
	    * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
	    * 下载
	    */
	    public static String localPicAdvancedGeneral(String filePath) {
	        // 请求url
	        String url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general";
	        try {
	            // 本地文件路径
//	            String filePath = "C:/Users/admin/Desktop/pic/index-img-4.png";
	            byte[] imgData = FileUtil.readFileByBytes(filePath);
	            String imgStr = Base64Util.encode(imgData);
	            String imgParam = URLEncoder.encode(imgStr, "UTF-8");

	            String param = "image=" + imgParam;

	            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
	            String accessToken = AuthService.getAuth();

	            String result = HttpUtil.post(url, accessToken, param);
	            System.out.println(result);
	            return result;
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return null;
	    }
	    /**
	     * 网络图图片识别
	     * @param filePath
	     * @return
	     */
	    public static String netPicAdvancedGeneral(String netPicPath) {
	        // 请求url
	        String url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general";
	        try {
	            // 本地文件路径
//	            String filePath = "C:/Users/admin/Desktop/pic/index-img-4.png";
	            byte[] imgData = NetImageUtils.getImgerFormNetByUrl(netPicPath);
	            String imgStr = Base64Util.encode(imgData);
	            String imgParam = URLEncoder.encode(imgStr, "UTF-8");

	            String param = "image=" + imgParam;

	            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
	            String accessToken = AuthService.getAuth();

	            String result = HttpUtil.post(url, accessToken, param);
	            System.out.println(result);
	            return result;
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return null;
	    }

	    public static void main(String[] args) {
	        AdvancedGeneral.localPicAdvancedGeneral("C:/Users/admin/Desktop/pic/index-img-4.png");
	    }
}
